﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Xml;
using System.Xml.Serialization;
using Sukul.SharePoint.Taxomatic;
using DTO = Sukul.SharePoint.Taxomatic.DTO;
using Sukul.SharePoint.Taxomatic.Service;

namespace Taxomatic.UnitTests
{
    [TestClass]
    public class TaxonomyTest
    {
        const string WEBSITE_URL = @"http://learningsukul.org:8082";
        const string SERVICE_URL = WEBSITE_URL + "/_vti_bin/Sukul.SharePoint.Taxomatic/TaxomaticService.svc";
        const string METHOD1_URL = SERVICE_URL + "/GetTerms";
        const string METHOD_TEST_URL = SERVICE_URL + "/Test";
        ICredentials _TestUser;

        public TaxonomyTest()
        {
            /* Set the test user here */
            _TestUser = CredentialCache.DefaultNetworkCredentials;            
            Helper.Instance.ActiveUser = _TestUser;
        }

        [TestMethod]
        public void XmlDeserialize()
        {
            /*DTO.TermStore store = new DTO.TermStore();
            System.IO.Stream xml = System.IO.File.OpenRead(@"D:\Temp\TaxomaticHandler.xml");
            XmlSerializer mySerializer = new XmlSerializer(typeof(DTO.TermStore));
            store = mySerializer.Deserialize(xml) as DTO.TermStore;
            */
            string xml = System.IO.File.ReadAllText(@"D:\Temp\TaxomaticHandler.xml");
            List<DTO.TermStore> stores = new List<DTO.TermStore>();
            stores = TaxonomyHelper.Instance.DeSerialize
                 (xml, stores.GetType()) as List<DTO.TermStore>;
        }

        [TestMethod]
        public void LoadTermStoreTest()
        {
            try
            {
                DTO.WebServiceResponse request = new DTO.WebServiceResponse();
                string data = "{\"request\": " + JsonConvert.SerializeObject(request) + "}";
                string s_response = Helper.Instance.CleanJSONObject(Helper.Instance.HttpResponse(METHOD1_URL, data, "POST"));

                /* Strong typed response object */
                var responseTypedObj = JsonConvert.DeserializeObject<DTO.WebServiceResponse>(s_response);

                /* This test simulates an error condition */
                Assert.IsTrue(responseTypedObj.IsSuccess);

                /* Retrieve the message body */
                JArray termStores = JArray.Parse(responseTypedObj.Payload.ToString());

                foreach (var store in termStores)
                {
                    Console.WriteLine(string.Format("Term Store Name: {0}", store["Name"].ToString()));

                    foreach (var group in (JArray)store["Groups"])
                    {
                        Console.WriteLine(string.Format("Group Name: {0}{1}", "\t", group["Name"].ToString()));
                        foreach (var termset in (JArray)group["Termsets"])
                        {
                            Console.WriteLine("Termset Name: {0}{1}", "\t\t", termset["Name"].ToString());
                            if (null != termset["Terms"])
                            {
                                OutputTerms((JArray)termset["Terms"], 3);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Assert.Fail(ex.Message);
            }
        }

        private void OutputTerms(JArray terms, int indent)
        {
            if (null != terms && terms.Count>0)
            {
                foreach (var term in terms)
                {
                    Console.WriteLine("Term Name: {0}{1}", new String('\t', indent), term["Name"].ToString());
                    if (null != term["Terms"] && ((JArray)term["Terms"]).Count > 0)
                    {
                        OutputTerms((JArray)term["Terms"], (indent + 1));
                    }
                }
            }
        }

    }
}
